using Crux.Core.SQL.Formatter.Language;
using static Crux.Core.SQL.Formatter.SqlFormatter;

namespace Crux.Core.Dapper.SqlGenerator;
/// <summary>
/// 
/// </summary>
public class SqlQuery
{
    /// <summary>
    ///     Initializes a new instance of the class.
    /// </summary>
    public SqlQuery(SqlProvider provider = SqlProvider.MSSQL)
    {
        SqlBuilder = new StringBuilder();
        this.Provider = provider;
    }


    /// <summary>
    ///     Initializes a new instance of the class.
    /// </summary>
    /// <param name="param">The param.</param>
    /// <param name="provider"></param>
    public SqlQuery(object param, SqlProvider provider = SqlProvider.MSSQL)
        : this(provider)
    {
        Param = param;
    }
    /// <summary>
    /// 
    /// </summary>
    public SqlProvider Provider { get; }
    /// <summary>
    ///     SqlBuilder
    /// </summary>
    public StringBuilder SqlBuilder { get; }

    /// <summary>
    ///     Gets the param
    /// </summary>
    public object? Param { get; private set; }

    /// <summary>
    ///     Gets the SQL.
    /// </summary>
    public virtual string GetSql()
    {
        Formatter formatter = Provider switch
        {
            SqlProvider.MSSQL => Of(Dialect.TSql),
            SqlProvider.PostgreSQL => Of(Dialect.PostgreSql),
            SqlProvider.MySQL => Of(Dialect.MySql),
            SqlProvider.SQLite => Of(Dialect.MySql),
            SqlProvider.Oracle => Of(Dialect.PlSql),
            _ => Of(Dialect.StandardSql)
        };

        return formatter.Format(SqlBuilder.ToString().Trim());
    }

    /// <summary>
    ///     Set alternative param
    /// </summary>
    /// <param name="param">The param.</param>
    public virtual void SetParam(object param)
    {
        Param = param;
    }
    /// <summary>
    ///
    /// </summary>
    /// <returns></returns>
    public override string ToString()
    {
        return GetSql();
    }
}